<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :Addtodo="Addtodo"/>
      <List :todos="todos" :deleteTodo="deleteTodo" :toggle="toggle"/>
      <Footer :todos="todos" :selectAll="selectAll" :clearCheckTodos="clearCheckTodos"/>
      <!-- <hr>
      <PropsTest name="zhangsan" myAge="-24"/> -->
    </div>
  </div>
</template>

<script>
import Header from './components/Header.vue';
import List from './components/List.vue';
import Footer from './components/Footer.vue';
// import PropsTest from './propsTest/PropsTest.vue';
import { nanoid } from "nanoid";

export default {
  data(){
    return{
      todos:[
        {id:nanoid(),title:"洗漱",completed:false},
        {id:nanoid(),title:"吃早餐",completed:false},
        {id:nanoid(),title:"上学",completed:false},
        {id:nanoid(),title:"吃午饭",completed:false},
        {id:nanoid(),title:"午休",completed:false},
      ]
    }
  },
  // 一般数据在哪里，修改就在哪里
  methods:{
    // 创建一个todo
    Addtodo(todo){
      todo.id = nanoid();
      this.todos.push(todo);
    },
    // 删除一个todo
    deleteTodo(index){
      this.todos.splice(index,1);
    },
    // 全选或者全不选
    selectAll(isCheck){
      this.todos.forEach(todo=>todo.completed = isCheck);
    },
    // 消除已完成
    clearCheckTodos(){
      this.todos = this.todos.filter(todo=>!todo.completed);
    },
    // 将一个指定的todo勾选的状态切换
    // 目的是不改变数据在哪里修改就在哪里的规定
    toggle(todo){
      todo.completed = !todo.completed;
    }
  },

  name: 'App',
  components: {
    Header,
    List,
    Footer,
    // PropsTest
  }
}
</script>

<style>
  .todo-container {
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
</style>
